package com.atguigu01.example;

import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectionTest {
    @Test
    public void t1(){
        //1.创建Person类的实例public Person()
        Person p1 = new Person();
//2.调用属性
//public int age;
  p1.age = 10;
        System.out.println(p1.age);
//3.调用方法
        //public void show()
         p1.show();
    }
    /*
     *反射
     * */
    @Test
    public void t2() throws Exception {
        //创建Person的实例
        Class<Person> clazz=Person.class;
        Person p1=clazz.newInstance();
        System.out.println(p1);

        //属性
        Field fieldage = clazz.getField("age");
        fieldage.set(p1,10);
        System.out.println(fieldage.get(p1));

        //方法
        Method methodshow = clazz.getMethod("show");
        methodshow.invoke(p1);

    }

    /*
    * 反射-私有结构
    * */
    @Test
    public void t3(){
        //调用私有的构造器

    }
}
